package com.flower.salary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flower.salary.common.utils.JwtUtil;
import com.flower.salary.common.utils.LocalUser;
import com.flower.salary.common.utils.LogLevel;
import com.flower.salary.common.utils.SalaryException;
import com.flower.salary.domain.entity.SysSalary;
import com.flower.salary.domain.entity.SysUser;
import com.flower.salary.domain.entity.SysUserSalary;
import com.flower.salary.mapper.SysSalaryMapper;
import com.flower.salary.mapper.SysUserSalaryMapper;
import com.flower.salary.service.SysLogService;
import com.flower.salary.service.SysSalaryService;
import javafx.scene.input.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-02-21 0:04
 */
@Service
public class SysSalaryServiceImpl implements SysSalaryService {

    @Autowired
    private SysLogService logService;

    @Autowired
    private SysUserSalaryMapper sysUserSalaryMapper;

    @Autowired
    private SysSalaryMapper sysSalaryMapper;

    @Override
    public List<SysUserSalary> getAll() {
        QueryWrapper q = new QueryWrapper();
        List<SysUserSalary> list = sysUserSalaryMapper.selectList(q);
        logService.insert("查询所有人工资", LogLevel.INFO);
        return list;
    }

    @Override
    public int update(SysUserSalary body) {
        int i = sysUserSalaryMapper.updateById(body);
        if(i==0){
            throw  new SalaryException("修改薪水失败");
        }
        logService.insert("修改"+ body.getName() + "薪水", LogLevel.DANGETOUS);
        return 0;
    }

    @Override
    public int create(SysUserSalary body) {
        // 修改状态 把未生成工资单 改成已生成工资单
        body.setState(1);
        int i = sysUserSalaryMapper.updateById(body);
        SysUser localUser = LocalUser.getLocalUser();

        SysSalary salary = new SysSalary();
        salary.setUserId(body.getId());
        salary.setBasicSalary(body.getBasicSalary());
        salary.setNetSalary(body.getNetSalary());
        salary.setBonus(body.getBonus());
        salary.setTaxDeduction(body.getTaxDeduction());
        salary.setName(body.getName());

        salary.setCreateTime(LocalDateTime.now());
        salary.setUserAdmin("刘唯卿");
        if(localUser != null){
            salary.setUserAdmin(localUser.getUserName());
        }
        int insert = sysSalaryMapper.insert(salary);
        if(insert==0){
            throw  new SalaryException("生成工资单失败");
        }
        logService.insert("生成工资单", LogLevel.INFO);
        return 0;
    }

    @Override
    public List<SysSalary> getUser(String token) {

        // 获取用户id
        int userId = JwtUtil.getUserId(token);
        QueryWrapper q = new QueryWrapper();
        q.eq("user_id",userId);
        List list = sysSalaryMapper.selectList(q);



        logService.insert("查询工资单", LogLevel.INFO);
        return list;
    }
}
